package dao;

import beans.*;
import java.rmi.RemoteException;
import javax.ejb.*;
import javax.naming.*;
import wrappers.CountryWrap;
import exceptions.DatabaseException;
import java.util.List;
import javax.rmi.PortableRemoteObject;

/**
 *
 * @author KAV
 */
public class CountryDAO implements BaseDAO {

    CountriesBeanRemoteHome home;

    public CountryDAO() {
        try {
            InitialContext ic = new InitialContext();
            Object objBean = ic.lookup("ejb/beanCountries");
            home = (CountriesBeanRemoteHome) PortableRemoteObject.narrow(objBean, CountriesBeanRemoteHome.class);
        } catch (NamingException ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    public void insert(Object obj) throws DatabaseException {
        try {
            CountryWrap e = (CountryWrap) obj;
            home.create(e.getTitle());
        } catch (CreateException ex) {
            throw new DatabaseException(ex);
        } catch (RemoteException ex) {
            throw new DatabaseException(ex);
        }
    }

    @Override
    public void deleteById(int id) throws DatabaseException {
        try {
            Object o = home.findByPrimaryKey(id);
            CountriesBeanRemote cbr = (CountriesBeanRemote) o;
            cbr.remove();
        } catch (RemoveException ex) {
            throw new DatabaseException(ex);
        } catch (RemoteException ex) {
            throw new DatabaseException(ex);
        } catch (FinderException ex) {
            throw new DatabaseException(ex);
        }catch (EJBException ex) {
            throw new DatabaseException(ex);
        }
    }

    @Override
    public void updateById(Object obj) throws DatabaseException {
        try {
            CountryWrap e = (CountryWrap) obj;
            CountriesBeanRemote cbr = (CountriesBeanRemote) home.findByPrimaryKey(e.getId());
            cbr.setTitle(e.getTitle());
        } catch (FinderException ex) {
            throw new DatabaseException(ex);
        } catch (RemoteException ex) {
            throw new DatabaseException(ex);
        }
    }

    @Override
    public List<Object> findAll() throws DatabaseException {
        List<Object> list = new java.util.LinkedList<Object>();
        try {
            List<Object> container = (List<Object>) home.findAll();
            for (Object o : container) {
                CountriesBeanRemote cbr = (CountriesBeanRemote) o;
                CountryWrap cw = new CountryWrap((Integer) cbr.getPrimaryKey(), cbr.getTitle());
                list.add((Object) cw);
            }
        } catch (RemoteException ex) {
            throw new DatabaseException(ex);
        } catch (FinderException ex) {
            throw new DatabaseException(ex);
        }
        return list;
    }

    @Override
    public Object findById(int id) throws DatabaseException {
        Object obj = new Object();
        try {
            Object o = home.findByPrimaryKey(id);
            CountriesBeanRemote cbr = (CountriesBeanRemote) o;
            CountryWrap cw = new CountryWrap((Integer) cbr.getPrimaryKey(), cbr.getTitle());
            obj = (Object) cw;
        } catch (RemoteException ex) {
            throw new DatabaseException(ex);
        } catch (FinderException ex) {
            throw new DatabaseException(ex);
        }
        return obj;
    }

    @Override
    public List<Object> findByNameOrSurname(String query) throws DatabaseException {
        throw new DatabaseException(new UnsupportedOperationException("Not supported yet."));
    }
}
